﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;

namespace CPPEI.ETL.WebApp
{
    public class Global : System.Web.HttpApplication
    {
        public static class WebApiConfig
        {
            public static void Register(RouteCollection routes)
            {
                routes.MapHttpRoute(
                    name: "ETL_Api",
                    routeTemplate: "api/etl/{controller}/{action}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
            }
        }

        protected void Application_Start(object sender, EventArgs e)
        {
            WebApiConfig.Register(System.Web.Routing.RouteTable.Routes);

            CPPEI.Logger.InitByLogDirectory(System.Configuration.ConfigurationManager.AppSettings["LogDirectory"]);

            CPPEI.Logger.InfoToTag("WEB", "Application_Start");

            CPPEI.ETL.Domain.Configs.Init();

            CPPEI.ETL.Domain.Cache.Init();

            CPPEI.ETL.Domain.TaskProxy.TaskProxyManager.Init(System.Configuration.ConfigurationManager.AppSettings["TaskDirectory"]);

            CPPEI.ETL.Domain.Services.EBSService.Init(HttpContext.Current.Server.MapPath("~/configs/ebs.config"), "EBSReceive.ashx");
            CPPEI.ETL.Domain.Services.EBSService.RegisterAllEventTask();

            string taskAgentFilePath = HttpContext.Current.Server.MapPath("~/bin/Monitor/CPPEI.ETL.Monitor.exe");

            CPPEI.ETL.Domain.Services.TaskService.StartTaskMonitor(Environment.MachineName, Environment.UserName, taskAgentFilePath);

            Logger.InfoToTag("WEB", "WebApplication Start finished!");
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}